﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Data;
using System.ComponentModel;
using EIA.Basic.Model;

namespace EIA.SLClient.PlatManage.PMMgr
{
    public class DicTypeListViewModel : SimpleMvvmToolkit.ViewModelBase<DicTypeListViewModel>
    {
        public DicTypeListViewModel()
        {

        }

        private void Busy(string msg = "等待...")
        {
            IsBusy = true;
            SendMessage<bool>(EIA.SLClient.PlatManage.Infrastructure.MessageTokens.DIC_BUSY_STATE, new SimpleMvvmToolkit.NotificationEventArgs<bool>(msg, true));
        }

        private void Free()
        {
            IsBusy = false;
            SendMessage<bool>(EIA.SLClient.PlatManage.Infrastructure.MessageTokens.DIC_BUSY_STATE, new SimpleMvvmToolkit.NotificationEventArgs<bool>("", false));
        }

        #region notifyprop

        protected bool _IsBusy;
        /// <summary>
        /// 
        /// </summary>
        public bool IsBusy
        {
            get { return _IsBusy; }
            set
            {
                _IsBusy = value;
                NotifyPropertyChanged(m => m.IsBusy);
            }
        }

        protected PagedCollectionView _DicTypeCollection;
        /// <summary>
        /// 注释
        /// </summary>
        public PagedCollectionView DicTypeCollection
        {
            get { return _DicTypeCollection; }
            set
            {
                _DicTypeCollection = value;
                NotifyPropertyChanged(m => m.DicTypeCollection);
            }
        }

        protected DicType _SelectedDicType;
        /// <summary>
        /// 注释
        /// </summary>
        public DicType SelectedDicType
        {
            get { return _SelectedDicType; }
            set
            {
                _SelectedDicType = value;
                NotifyPropertyChanged(m => m.SelectedDicType);
                SendMessage<DicType>(EIA.SLClient.PlatManage.Infrastructure.MessageTokens.DIC_TYPE_SELECTED, new SimpleMvvmToolkit.NotificationEventArgs<DicType>("", value));
            }
        }

        #endregion
        #region command

        #region RetrieveItemsCommand

        private SimpleMvvmToolkit.DelegateCommand _RetrieveItemsCommand = null;
        public SimpleMvvmToolkit.DelegateCommand RetrieveItemsCommand
        {
            get
            {
                if (_RetrieveItemsCommand == null)
                {
                    _RetrieveItemsCommand = new SimpleMvvmToolkit.DelegateCommand(RetrieveItems, CanExcuteRetrieveItems);
                } return _RetrieveItemsCommand;
            }
            set { _RetrieveItemsCommand = value; NotifyPropertyChanged(m => m.RetrieveItemsCommand); }
        }

        public void RetrieveItems()
        {
            EIA.Basic.BLL.ClientProxy.XDictProxy proxy = new Basic.BLL.ClientProxy.XDictProxy();
            Busy();
            proxy.ListType(0, (types) =>
            {
                Free();
                DicTypeCollection = new PagedCollectionView(types);

            }, (ex) =>
            {
                Free();
                System.Windows.MessageBox.Show("获取字典类型列表发生错误！", "提示", System.Windows.MessageBoxButton.OK);
            });
        }

        public bool CanExcuteRetrieveItems()
        {
            return true;
        }

        #endregion

        #endregion

        public void OnLoad()
        {
            if (DicTypeCollection == null) RetrieveItems();
        }
    }
}